package com.atguigu.mall.mallmember.service.impl;

import com.atguigu.mall.mallmember.dao.MemberLevelDao;
import com.atguigu.mall.mallmember.vo.MemberRegistVo;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.mall.mallmember.dao.MemberDao;
import com.atguigu.mall.mallmember.entity.MemberEntity;
import com.atguigu.mall.mallmember.service.MemberService;

@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity>
    implements MemberService {

  @Autowired MemberLevelDao memberLevelDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<MemberEntity> page =
        this.page(new Query<MemberEntity>().getPage(params), new QueryWrapper<MemberEntity>());

    return new PageUtils(page);
  }

  @Override
  public void regist(MemberRegistVo vo) {
    MemberDao memberDao = this.baseMapper;
    MemberEntity entity = new MemberEntity();

    // 密码需要加密存储；随机值 加盐：$1$+8位字符
//    String md5Crypt = Md5Crypt.md5Crypt(vo.getPassword().getBytes(), vo.getUserName());

    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    String password = passwordEncoder.encode(vo.getPassword());
    entity.setPassword(password);

    memberDao.insert(entity);
  }
}
